from sqlalchemy import Column, Integer, String, Float, ForeignKey, Boolean, JSON
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import create_async_engine
from config import Settings
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
settings = Settings()
engine = create_engine(settings.database_url, echo=True)

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    username = Column(String, index=True, unique=True)
    total_contribution = Column(Float)
    contributions = relationship("Contribution", backref="user")
    def __repr__(self):
        return f"<User {self.username}>, <Total Contribution {self.total_contribution}>, <Contributions {self.contributions}>"
    def __tableheader__(self):
        return f"user_id, username, total_contribution, contributions"
    def __tablebody__(self):
        return f"{self.id}, {self.username}, {self.total_contribution}, {self.contributions}"
class Contribution(Base):
    __tablename__ = "contributions"

    id = Column(Integer, primary_key=True)
    user_id = Column(Integer, ForeignKey("users.id"))
    project_id = Column(Integer, ForeignKey("projects.id"))
    company_id = Column(Integer, ForeignKey("sub_companies.id"))
    value = Column(Float)

class SubCompany(Base):
    __tablename__ = "sub_companies"

    id = Column(Integer, primary_key=True)
    name = Column(String, index=True, unique=True)
    market_cap = Column(Float)

class Project(Base):
    __tablename__ = "projects"

    id = Column(Integer, primary_key=True)
    company_id = Column(Integer, ForeignKey("sub_companies.id"))
    total_number = Column(Integer)
    project_name = Column(String)

class Plan(Base):
    __tablename__ = "plans"

    id = Column(Integer, primary_key=True)
    project_id = Column(Integer, ForeignKey("projects.id"))
    plan_name = Column(String)
    plan_content = Column(String)
    admin_id = Column(Integer, ForeignKey("users.id"))
    participants = Column(JSON)
    votes = Column(Integer)
    stage = Column(Integer)
    status = Column(String)

